﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DadayunApi.Model;
using Newtonsoft.Json;
using RestSharp;

namespace DadayunApi
{
    public class BaseApi : Api
    {
        public BaseApi(AppAuthBase authBase, string baseUri) : base(authBase, baseUri)
        {
        }
        /// <summary>
        /// 返回服务器时间
        /// </summary>
        /// <returns>如果失败，则返回DateTime.MinValue</returns>
        public DateTime GetServerTime()
        {
            DateTime time = DateTime.MinValue;
            string uri = "v1/date";
            var client = new RestClient(baseUri);
            var request = new RestRequest(uri, Method.GET);
            request.AddHeader("Authorization", "Bearer " + token.Access_token);
            IRestResponse<DateTime> response = client.Execute<DateTime>(request);
            responseJson = response.Content;
            if (response.IsSuccessful)
            {
                time = response.Data;
            }
            return time;
        }
        /// <summary>
        ///  获取指定类别下的字典数据源
        /// </summary>
        /// <param name="dictIdOrName">字典ID或中文名</param>
        /// <returns></returns>
        public List<IdName> GetDictionaryItems(string dictIdOrName)
        {
            List<IdName> list = null;
            var uri = "/v1/data/dictionary_types/" + dictIdOrName + "/datas";
            var client = new RestClient(baseUri);
            var request = new RestRequest(uri, Method.GET);
            request.AddHeader("Authorization", "Bearer " + token.Access_token);
            IRestResponse response = client.Execute(request);
            if (response.IsSuccessful)
            {
                responseJson = response.Content;
                list = JsonConvert.DeserializeObject<List<IdName>>(responseJson);
            }
            return list;
        }
        /// <summary>
        /// 获取区域
        /// </summary>
        /// <param name="parentId">所属父区域Id，默认值为A0000000"</param>
        /// <returns>该区域下属区域清单</returns>
        public List<Area> GetAreas(string parentId = "A0000000")
        {
            List<Area> list = null;
            var uri = "/v1/data/areas?parentId=" + parentId;
            var client = new RestClient(baseUri);
            var request = new RestRequest(uri, Method.GET);
            request.AddHeader("Authorization", "Bearer " + token.Access_token);
            IRestResponse response = client.Execute(request);
            if (response.IsSuccessful)
            {
                responseJson = response.Content;
                list = JsonConvert.DeserializeObject<List<Area>>(responseJson);
            }
            return list;
        }
        /// <summary>
        /// 获取菜单列表
        /// </summary>
        /// <param name="parentId">父节点ID，默认没有，为根</param>
        /// <param name="isMobile">是否移动端菜单</param>
        /// <param name="getLeaf">是否获取叶子节点，默认为false不获取</param>
        /// <returns>返回菜单结构，叶子节点存在children中</returns>
        public List<MenuItem> GetMenuItems(string parentId = null, bool isMobile = false, bool getLeaf = false)
        {
            List<MenuItem> list = null;
            var uri = "/v1/menus?isMobile=" + isMobile + "&leaf=" + getLeaf;
            if (!string.IsNullOrEmpty(parentId))
            {
                uri += "&parentId=" + parentId;
            }
            var client = new RestClient(baseUri);
            var request = new RestRequest(uri, Method.GET);
            request.AddHeader("Authorization", "Bearer " + token.Access_token);
            IRestResponse response = client.Execute(request);
            if (response.IsSuccessful)
            {
                responseJson = response.Content;
                list = JsonConvert.DeserializeObject<List<MenuItem>>(responseJson);
            }
            return list;
        }

    }
}
